package cn.blog.dao.back;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cn.blog.dao.conn.DataManager;
import cn.blog.dao.conn.ParamSet;
import cn.blog.dao.conn.RowDatas;
import cn.blog.entity.origin.Article;
import cn.blog.entity.origin.Article2;
import cn.blog.entity.origin.ClassBean;
import cn.blog.entity.origin.User;

/**
 * 后台博文管理DAO层
 * @author zjk
 *1.查询所有博客文章
 *2.根据博客账号或者文章类型查询文章
 *3.根据文章id删除文章
 */
public class ArticleDao2 extends DataManager{
	private RowDatas<Article2> artilce;//文章对象
	private RowDatas<String> claName;//文章类型名称
	
	/**
	 * 无参构造，初始化
	 */
	public ArticleDao2(){
		artilce = new ArtData();
		claName = new ClaNameData();
	}
	/**
	 * 1.查询所有博客文章
	 * @return
	 */
	public List<Article2> getArticleAll(){
		String sql = "SELECT a.art_id,u.user_account,a.art_title,ac.class_name,a.art_time FROM  "
				+ "(tb_article a LEFT JOIN tb_user u on a.art_user_id=u.user_id)"
				+ "LEFT JOIN tb_article_class ac on  a.art_class_id=ac.class_id";
		return queryList(sql, artilce);
		
	}
	
	/**
	 * 2.根据博客账号或者文章类型查询文章s
	 * @return
	 */
	public List<Article2> getArticle(String account,String cla){
		String sql= "SELECT a.art_id, u.user_account,a.art_title,ac.class_name,a.art_time FROM "
				+ " (tb_article a LEFT JOIN tb_user u on a.art_user_id=u.user_id)LEFT JOIN "
				+ " tb_article_class ac on  a.art_class_id=ac.class_id WHERE u.user_account "
				+ "like '%"+account+"%' or class_name like '%"+cla+"%'";

		return queryList(sql, artilce);
		
	}
	/**
	 * 3.根据文章名称删除文章
	 * @param id
	 * @return
	 */
	public int delarticle(int id){
		String sql = "DELETE FROM tb_article WHERE art_id= ?";
		return updateExecuted(sql, new ParamSet(id));
	}
	
	/**
	 * 获取文章数据
	 * @author zjk
	 *
	 */
	public class ArtData implements RowDatas<Article2>{
		@Override
		public Article2 getRowData(ResultSet rs) throws SQLException {
			Article2 artilce = new Article2();
			artilce.setArtId(rs.getInt(1));
			artilce.setUserAccount(rs.getString(2));
			artilce.setTitle(rs.getString(3));
			artilce.setArtClass(rs.getString(4));
			artilce.setArtTime(rs.getDate(5));
			return artilce;
		
	}

	
	}
	public class ClaNameData implements RowDatas<String>{

		@Override
		public String getRowData(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			return rs.getString(1);
		}
		
	}
	
	
}
